$(function() {
	// 初始化表格
	$("#dataTable").jqxDataTable({
		width : "100%",
		source : getSource(),
		pageable : true,
		serverProcessing : true,
		pageSize : 15,
		pagerButtonsCount : 10,
		columnsResize : true,
		sortable : true,
		selectionMode : "singleRow",
		columns : [ {
			text : "序号",
			width : "4%",
			dataField : "id",
			cellsRenderer : function(row) {
				return (row + 1).toString();
			}
		}, {
			text : "订单号",
			dataField : "orderid"
		}/*, {
			text : "商店",
			width : "10%",
			dataField : "shop_name"
		}*/, {
			text : "用户名",
			width : "12%",
			dataField : "user_name"
		}, {
			text : "总金额",
			width : "12%",
			dataField : "aggregate_amount"
		}, {
			text : "实际支付金额",
			width : "12%",
			dataField : "actual_price"
		}, {
			text : "钱包支付金额",
			width : "12%",
			dataField : "walletpay"
		}
		
		
		, {
			text : "类型",
			dataField : "order_type",
			width : "12%",
			cellsRenderer : function(row, column, value, rowData) {
				return getOrder_type(value);
			}
		}/*, {
			text : "下单时间",
			dataField : "confirmtime",
			width : "12%",
			cellsRenderer : function(row, column, value, rowData) {
				if (value === "" || value === undefined || value === null) {
					return "";
				}
				var date = value instanceof Date ? value : new Date(value);
				return date.Format("yyyy-MM-dd hh:mm:ss");
			}
		}*/, {
			text : "支付时间",
			dataField : "paytime",
			width : "15%",
			cellsRenderer : function(row, column, value, rowData) {
				if (value === "" || value === undefined || value === null) {
					return "";
				}
				var date = value instanceof Date ? value : new Date(value);
				return date.Format("yyyy-MM-dd hh:mm:ss");
			}
		}, {
			text : "创建时间",
			dataField : "createtime",
			width : "15%",
			cellsRenderer : function(row, column, value, rowData) {
				if (value === "" || value === undefined || value === null) {
					return "";
				}
				var date = value instanceof Date ? value : new Date(value);
				return date.Format("yyyy-MM-dd hh:mm:ss");
			}
		} ],
	});

	/**
	 * 双击查看详细
	 */
	$("#dataTable").on("rowDoubleClick", function(event) {
		var args = event.args;
		var row = args.row;
		var index = args.index;
		viewOrderDetailed(row);
	});
	
	/**
	 * 查看
	 */
	$("#view").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			viewOrderDetailed(rows[0]);
		}
	});
	
	//同意退款
	$("#agreedRefund").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			agreedRefund(rows[0].uuid);
		}
	});

	//拒绝退款
	$("#refusedRefund").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			refusedRefund(rows[0].uuid);
		}
	});

	/**
	 * 搜索
	 */
	$("#search").click(function() {
		var orderId = $("#orderId").val();
		var json = {
			orderId : orderId
		}
		$("#dataTable").jqxDataTable({
			source : getSource(json)
		});
	});

});

//同意退款
function agreedRefund(uuid) {
/*	var sb = new Array();
	sb.push('<form id="agreedRefundForm" name="alipaysubmit" action="');
	sb.push('/manager/order/agreedRefund.do');
	sb.push('" method="post">');
	sb.push('<input type="hidden" name="uuid" value="');
	sb.push(uuid);
	sb.push('"></form>');
	$("body").append(sb.join(""));
	$("#agreedRefundForm").submit();*/
	$.ajax({
		type : "POST",
		url : "/manager/order/agreedRefund.do",
		dataType : "jsonp",
		data : {
			uuid : uuid
		},
		success : function(result) {
			if (result.code === 1) {
				layer.open({
				    btn: ['OK'],
				    content:result.msg,
				    yes: function(index){
				    	location=location;
				    }
				});
			}else{
				layer.msg(result.msg);
			}
		},
		error : function(XHR, type) {
			layer.msg(type);
		}
	});
}

//拒绝退款
function refusedRefund(uuid) {
	$.ajax({
		type : "POST",
		url : "/manager/order/refusedRefund.do",
		dataType : "jsonp",
		data : {
			uuid : uuid
		},
		success : function(result) {
			if (result.code === 1) {
				layer.open({
				    btn: ['OK'],
				    content:result.msg,
				    yes: function(index){
				    	location=location;
				    }
				});
			}else{
				layer.msg(result.msg);
			}
		},
		error : function(XHR, type) {
			layer.msg(type);
		}
	});
}

/**
 * 查看商品详细
 * 
 * @param row
 */
function viewOrderDetailed(row) {
	_editerIndex = layer.open({
		id : "expressEditer",
		title : "订单详细",
		type : 1,
		zIndex : 99999999,
		shadeClose : false,
		skin : 'layui-layer-rim',
		area : [ '1024px', '600px' ],
		content : $("#content").html(),
		success : function(e) {
			eConetnt = $(e);
		}
	});
	// $("#uuid").val(row.uuid);
	eConetnt.find("#orderid").val(row.orderid);
	eConetnt.find("#user_name").val(row.user_name);
	eConetnt.find("#consignee_name").val(row.consignee_name);
	eConetnt.find("#express_name").val(row.express_name);
	eConetnt.find("#aggregate_amount").val(row.aggregate_amount);
	eConetnt.find("#actual_price").val(row.actual_price);
	eConetnt.find("#status").val(getStatus(row.status));
	eConetnt.find("#confirmtime").val(getDate(row.confirmtime));
	eConetnt.find("#paytime").val(getDate(row.paytime));
	eConetnt.find("#createtime").val(getDate(row.createtime));
	detailed_datatable(row.uuid);
}

/**
 * 初始化商品详细
 */
function detailed_datatable(uuid) {
	// 设置数据源
	var tableSource = new $.jqx.dataAdapter({
		dataType : "json",
		dataFields : [ {
			name : "id",
			type : "int"
		}, {
			name : "goods_name",
			type : "string"
		}, {
			name : "price",
			type : "double"
		}, {
			name : "quantity",
			type : "int"
		}, {
			name : "subtotal",
			type : "double"
		} ],
		id : "uuid",
		url : "/manager/order/getOrderDetailedByUuid.do?uuid=" + uuid
	});
	// 初始化表格
	eConetnt.find("#detailed-datatable").jqxDataTable({
		width : "95%",
		source : tableSource,
		pageable : true,
		serverProcessing : true,
		pageSize : 3,
		pagerButtonsCount : 10,
		columnsResize : true,
		sortable : true,
		columns : [ {
			text : "序号",
			width : 50,
			cellsRenderer : function(row) {
				return (row + 1).toString();
			}
		}, {
			text : "商品名称",
			dataField : "goods_name"
		}, {
			text : "价格",
			dataField : "price"
		}, {
			text : "数量",
			dataField : "quantity"
		}, {
			text : "小计",
			dataField : "subtotal",
		} ],
	}).css("min-height", "320px");
}

/**
 * 获取数据源
 */
function getSource(json) {
	var url = "/manager/order/getRefundOrderList.do?test=1";
	if (json !== undefined) {
		if (json.orderId !== undefined && json.orderId !== null
				&& json.orderId !== "") {
			url += "&orderId=" + json.orderId;
		}
	}
	// 设置数据源
	var tableSource = new $.jqx.dataAdapter({
		dataType : "json",
		dataFields : [ {
			name : "id",
			type : "int"
		}, {
			name : "uuid",
			type : "string"
		}, {
			name : "orderid",
			type : "string"
		}, {
			name : "shop_name",
			type : "string"
		}, {
			name : "user_name",
			type : "string"
		}, {
			name : "aggregate_amount",
			type : "double"
		}, {
			name : "actual_price",
			type : "double"
		},{
			name : "walletpay",
			type : "double"
		},
		
		
		{
			name : "order_type",
			type : "int"
		}, {
			name : "confirmtime",
			type : "date"
		}, {
			name : "paytime",
			type : "date"
		}, {
			name : "createtime",
			type : "date"
		} ],
		id : "uuid",
		url : url
	}, {
		formatData : function(data) {
			data.pagenum = data.pagenum + 1;
			return data;
		}
	});
	return tableSource;
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}

/**
 * 获取状态
 * 
 * @param value
 * @returns {String}
 */
function getStatus(value, html) {
	var newvalue = "";
	switch (value) {
	case 0:
		newvalue = "待支付";
		if (html) {
			newvalue = '<span style="color:#CC0000;">' + newvalue + '</span>';
		}
		break;
	case 1:
		newvalue = "待发货";
		if (html) {
			newvalue = '<span style="color:#CCCC00;">' + newvalue + '</span>';
		}
		break;
	case 2:
		newvalue = "已发货";
		if (html) {
			newvalue = '<span style="color:#009933;">' + newvalue + '</span>';
		}
		break;
	case 3:
		newvalue = "已配送";
		if (html) {
			newvalue = '<span style="color:#0099FF;">' + newvalue + '</span>';
		}
		break;
	case 4:
		newvalue = "已签收";
		if (html) {
			newvalue = '<span style="color:#00CC66;">' + newvalue + '</span>';
		}
		break;
	case -1:
		newvalue = "已取消";
		if (html) {
			newvalue = '<span style="color:#FF6600;">' + newvalue + '</span>';
		}
		break;
	}
	return newvalue;
}

/**
 * 获取状态
 * 
 * @param value
 * @returns {String}
 */
function getOrder_type(value) {
	var newvalue = "";
	if (value == 1) {
		newvalue = "T10商城";
	} else {
		newvalue = "折扣空间";
	}
	return newvalue;
}
 
/**
 * 格式化日期
 * 
 * @param value
 * @returns
 */
function getDate(value) {
	if (value === "" || value === undefined || value === null) {
		return "";
	}
	var date = value instanceof Date ? value : new Date(value);
	return date.Format("yyyy-MM-dd hh:mm:ss");
}